$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        schoolId:null,
        init:function () {
            this.schoolId = xhTool.getQuery('schoolId');
            this.funObj().loadProd($('#list_prodType').val());
            this.funObj().triggerSortType($('#list_sortType').val());
            // this.funObj().loadSortType($('#list_type').val());
            this.funObj().loadPayLine();

            this.listener();
        },
        listener:function () {
            var $this = this;
            //类型改变
            $('#list_prodType').change(function () {
                $this.funObj().loadProd($(this).val());
            });
            //排序类型
            $('#list_sortType').change(function () {
                $this.funObj().triggerSortType($(this).val());
            });

            //搜索
            $('#btn_view').click(function () {
                $this.funObj().loadPayLine();
            });
            //类型
            $('#list_type').change(function () {
                $this.funObj().loadSortType($('#list_type').val());
            })

        },
        funObj:function () {
            var $this = this;
            return {
                loadPayLine:function () {
                    var params = $this.funObj().getParams();
                    swal({
                        title: '统计执行中！请耐心等待...',
                        showConfirmButton: false,
                        type: 'success',
                        allowOutsideClick: false
                    });
                    setTimeout(function () {
                        swal.close();
                    }, 30 * 1000);
                    xhExtra.ajaxRequest('ims/stats/getSource', params, {check:false},function (source) {
                        xhExtra.setBtnNormal($('#btn_view'));
                        swal.close();
                        if (xhExtra.checkError(source)) {
                            return false;
                        }
                        if (source.yMin) {
                            (new xhChart()).line('chart', source, {showTool: true, xFontSize: 10},{
                                yAxis: [{
                                    type: 'value',
                                    splitArea: {show: true},
                                    name: '总量',
                                    min: source.yMin,
                                    position: 'left',
                                    axisLabel: {
                                        formatter: '{value}',
                                        textStyle: {
                                            fontSize: 12
                                        }
                                    }
                                }]
                            });
                        } else  {
                            (new xhChart()).line('chart', source, {showTool: true, xFontSize: 10});
                        }
                    });
                },
                loadProd:function (typeId) {
                    xhExtra.ajaxRequest('ims/stats/getProdByType',{typeId:typeId},{}, function (res) {
                        var str = '<option value="0" selected>全部商品</option>';
                        $.each(res,function (i,text) {
                            str +='<option value="'+i+'">'+text+'</option>';
                        });
                        $('#list_prod').empty().append(str);
                    })
                },
                loadSortType:function (type) {
                    xhExtra.ajaxRequest('ims/stats/getSortType', {type:type},{}, function (res) {
                        var str = '';
                        $.each(res,function (id,text) {
                            str +='<option value="'+id+'">'+text+'</option>';
                        });
                        $('#list_sortType').empty().append(str);
                        $this.funObj().triggerSortType($('#list_sortType').val());
                    })
                },
                getParams:function () {
                    return {
                        startTime: $('#startTime').val(),
                        endTime: $('#endTime').val(),
                        province: $('#list_province',parent.document).find("option:selected").text(),
                        city: $('#list_city',parent.document).find("option:selected").text(),
                        county: $('#list_county',parent.document).find("option:selected").text(),
                        prodType: $('#list_prodType').val(),
                        prod: $('#list_prod').val(),
                        statsType: $('#stats_type',parent.document).val(),
                        type: $('#list_type').val(),
                        sortType: $('#list_sortType').val(),
                        schoolId:$this.schoolId
                    };
                },
                triggerSortType:function ($sortType) {
                    var dom = $('#list_prod');
                    if ($sortType == 'iProdId') {
                        dom.val(0); //切换为全部商品 并且 disable
                        dom[0].disabled = true;
                    } else {
                        dom[0].disabled = false;
                    }
                    var prodType = $('#list_prodType');
                    if ($sortType == 'iDevType') {
                        $('#list_prodType option').filter(function(){return $(this).text()=="平板";}).prop("selected",true);
                        $this.funObj().loadProd(prodType.val());
                        prodType[0].disabled = true;
                    } else {
                        prodType[0].disabled = false;
                    }
                }
            }
        }
    }
};